package com.person.leetcode.editor.cn;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 8.字符串转换整数 (atoi)
 * 2022-04-26 16:40:43
 **/
public class Sequence_0008_StringToIntegerAtoi {
    public static void main(String[] args) {
        Solution solution = new Sequence_0008_StringToIntegerAtoi().new Solution();
        String s = "0123";
        String substring = s.substring(0, 0);
        System.out.println(substring);
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {

        class Auto{
            int flag = 1;
            long digit = 0;
            String state = "start";
            HashMap<String,String[]> map = new HashMap<String,String[]>(){{
//      ' ' 0
//      +/- 1
//      数字 2
//      其他 3
                put("start",new String[]{"start","sign","digit","end"});
                put("sign",new String[]{"end","end","digit","end"});
                put("digit",new String[]{"end","end","digit","end"});
                put("end",new String[]{"end","end","end","end"});
            }};

            int getChar(char c){
                if (c == ' ')return 0;
                if (c == '-' || c == '+')return 1;
                if (Character.isDigit(c)) return 2;
                return 3;
            }

            void get(char c){
                this.state = map.get(state)[getChar(c)];
                if ("digit".equals(this.state)){
                    this.digit = this.digit*10+c-'0';
                    //都转为正数
                    this.digit = this.flag == 1?Math.min(this.digit,(long)Integer.MAX_VALUE):Math.min(this.digit,-(long)Integer.MIN_VALUE);
                }else if ("sign".equals(this.state)){
                    this.flag = c == '+'?1:-1;
                }
            }
        }

        public int myAtoi(String str) {
            Auto auto = new Auto();
            for (char c : str.toCharArray()) {
                auto.get(c);
            }
            return auto.flag*(int)auto.digit;
        }



//    public int myAtoi(String s) {
//
//        //垃圾题目
////        String s1 = s.replaceAll("\\s", "");
////        String replaceString = s1.replaceAll("[a-zA-Z]", "");
////        Long aLong = Long.valueOf(replaceString);
////        if (aLong < Integer.MIN_VALUE){
////            return Integer.MIN_VALUE;
////        }else if (aLong > Integer.MAX_VALUE){
////            return Integer.MAX_VALUE;
////        }else{
////            return Integer.valueOf(replaceString);
////        }
//
//        return 0;
//    }
//        public int myAtoi(String str) {
//            Automaton automaton = new Automaton();
//            int length = str.length();
//            for (int i = 0; i < length; ++i) {
//                automaton.get(str.charAt(i));
//            }
//            return (int) (automaton.sign * automaton.ans);
//        }
    }

//    class Automaton {
//        public int sign = 1;
//        public long ans = 0;
//        private String state = "start";
//        //定义状态映射
//        private Map<String, String[]> table = new HashMap<String, String[]>() {{
//            put("start", new String[]{"start", "signed", "in_number", "end"});
//            put("signed", new String[]{"end", "end", "in_number", "end"});
//            put("in_number", new String[]{"end", "end", "in_number", "end"});
//            put("end", new String[]{"end", "end", "end", "end"});
//        }};
//
//        public void get(char c) {
//            //状态变换
//            state = table.get(state)[get_col(c)];
//            if ("in_number".equals(state)) {
//                ans = ans * 10 + c - '0';//字符相减获取差值
//                ans = sign == 1 ? Math.min(ans, (long) Integer.MAX_VALUE) : Math.min(ans, -(long) Integer.MIN_VALUE);
//            } else if ("signed".equals(state)) {
//                sign = c == '+' ? 1 : -1;
//            }
//        }
//
//        //定义动作
//        private int get_col(char c) {
//            if (c == ' ') {
//                return 0;
//            }
//            if (c == '+' || c == '-') {
//                return 1;
//            }
//            if (Character.isDigit(c)) {
//                return 2;
//            }
//            return 3;
//        }
//    }
//leetcode submit region end(Prohibit modification and deletion)

}
